package xiaohu.day5.HJ50;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

public class Main2 {

    // 存放算数优先级
    public static HashMap<Character, Integer> map = new
            HashMap<Character, Integer>() { //这里泛型必须写
                // 声明优先级
                {
                    put('-', 1);
                    put('+', 1);
                    put('*', 2);
                    put('/', 2);
                }
            };

    public static void main(String[] args) {
        int target = 0;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine().replaceAll("\\[", "(")
                    .replaceAll("\\]", ")")
                    .replaceAll("\\{", "(")
                    .replaceAll("\\}", ")");
            str = replaceBracket(str);
            if (str.equals("")) {
                System.out.println("0");
                return;
            }
            int targetNum = calculateBracket(str);
            System.out.println(targetNum);
        }
    }

    private static String replaceBracket(String str) {
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if (str.charAt(i) == ('(')) {
                int count = 1;
                for (int j = i + 1; j < length; j++) {
                    if (str.charAt(j) == ('(')) {
                        count++;
                    } else if (str.charAt(j) == (')')) {
                        count--;
                    }
                    if (count == 0) {
                        String bracket = str.substring(i+1, j);
                        int bracketValue = calculateBracket(bracket);
                        str = str.replace(str.substring(i, j+1), String.valueOf(bracketValue));
                        length=str.length();
                        break;
                    }
                }
            }
        }
        return str;
    }

    private static int calculateBracket(String bracket) {
/*        if (bracket.contains("(")) {
            String s = replaceBracket(bracket);
        }*/
        Stack<Integer> integerStack = new Stack<>();
        Stack<Character> charactersStack = new Stack<>();
        int number = 0;
        boolean isNumber = false;
        for (int i = 0; i < bracket.length(); i++) {
            char ch = bracket.charAt(i);
            if (!Character.isDigit(ch)) {
                if (isNumber) {
                    integerStack.push(number);
                    number = 0;
                    isNumber = false;
                }
                if (!charactersStack.empty()) {
                    Character peek = charactersStack.peek();
                    while (map.containsKey(peek) && map.get(peek)>=map.get(bracket.charAt(i))) {
                        //需要弹出栈顶符号并计算
                        peek=charactersStack.pop();
                        Integer rightNum = integerStack.pop();
                        Integer leftNum = integerStack.pop();
                        String cal = cal(leftNum, rightNum, peek);
                        integerStack.push(Integer.valueOf(cal));
                    }
                }
                charactersStack.push(bracket.charAt(i));
            } else {
                 number = number*10+(ch-'0');
                 isNumber = true;
            }
        }
        while (!charactersStack.empty()) {
            Character pop = charactersStack.pop();
            Integer rightNum = integerStack.pop();
            Integer leftNum = integerStack.pop();
            String cal = cal(leftNum, rightNum, pop);
            integerStack.push(Integer.valueOf(cal));
        }
        return integerStack.pop();
    }

    private static String cal(Integer leftNum, Integer rightNum, Character peek) {
        switch (peek) {
            case '+':return String.valueOf(leftNum+rightNum);
            case '-':return String.valueOf(leftNum-rightNum);
            case '*':return String.valueOf(leftNum*rightNum);
            case '/':return String.valueOf(leftNum/rightNum);
        }
        return String.valueOf(0);
    }
}
